﻿using System.Text.RegularExpressions;

namespace Little.Soldier.SWIFT.Validation
{
  public class ValidationRule_RegEx : ValidationRuleBase
  {
    private string U0001 = string.Empty;

    public ValidationRule_RegEx(string regExValue)
    {
      this.U0001 = regExValue;
    }

    public ValidationRule_RegEx(string regExValue, char delimiter, int count)
    {
      this.U0001 = regExValue;
      this.Delimiter = delimiter;
      this.Count = count;
    }

    public ValidationRule_RegEx(string regExValue, int startIndex)
    {
      this.U0001 = regExValue;
      this.StartIndex = startIndex;
    }

    public ValidationRule_RegEx(string regExValue, int startIndex, int length)
    {
      this.U0001 = regExValue;
      this.StartIndex = startIndex;
      this.Length = length;
    }

    public override ValidationError[] Validate(string value)
    {
      if (new Regex(this.U0001).IsMatch(value))
        return new ValidationError[0];
      if (string.IsNullOrEmpty(this.ErrorDescription))
        return new ValidationError[1]
        {
          new ValidationError(ValidationErrorType.UnrecognizedValue, string.Format("Value '{0}' has wrong format.", (object) value))
        };
      else
        return new ValidationError[1]
        {
          new ValidationError(ValidationErrorType.UnrecognizedValue, this.ErrorDescription)
        };
    }
  }
}
